package com.zcsy.commons.util;


import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;


/**
 * @Title: DigestUtil.java
 * @Package： com.zcsy.web.websocket
 * @Description: base64/MD5加密解密算法工具类
 * @author wangziliang 460694546@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年7月1日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
public class DigestUtil {
    private static final sun.misc.CharacterEncoder encoder = new sun.misc.BASE64Encoder();

    private static final sun.misc.CharacterDecoder decoder = new sun.misc.BASE64Decoder();

    private static final String radStr = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";// 排除0
                                                                              // 和o由于这2个字符用户不好判断

    /**
     * @Description 返回得到4个字符<br>
     * @return
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static String getRandCode() {
        Random random = new Random();
        StringBuilder rand = new StringBuilder();
        for (int i = 0; i < 4; i++ ) {
            rand.append(String.valueOf(radStr.charAt(random.nextInt(30))));
        }
        return rand.toString();
    }

    public final static String getUuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * @Description 用BASE64算法加密字符串<br>
     * @param source
     * @param encoding
     * @return
     * @throws Exception
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public final static String base64Encrypt(String source, String encoding)
        throws Exception {
        String ciphertext = null;
        if (encoding == null)
            ciphertext = encoder.encode(source.getBytes());
        else
            ciphertext = encoder.encode(source.getBytes("encoding"));
        return ciphertext;
    }

    /**
     * @Description 用BASE64算法解密字符串<br>
     * @param ciphertext
     * @param encoding
     * @return
     * @throws Exception
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public final static String base64Decrypt(String ciphertext, String encoding)
        throws Exception {
        // String source = null;
        if (encoding == null)
            ciphertext = new String(decoder.decodeBuffer(ciphertext));
        else
            ciphertext = new String(decoder.decodeBuffer(ciphertext), encoding);
        return ciphertext;
    }

    /**
     * @Description 功能：加密 输入：要加密的串 输出：加密后的串<br>
     * @param src
     * @return
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public final static String MD5(String src) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f'};
        try {
            byte[] strTemp = src.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++ ) {
                byte byte0 = md[i];
                str[k++ ] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++ ] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args)
        throws Exception {}

}